import THREE = require("three");
import { sceneOption as opts } from "../state";

export class Generator {
  private barrierNum: number;
  constructor() {
    this.barrierNum = 0;
  }

  /**
   * 生成地面
   */
  public generateGround(): void {
    const geometry = new THREE.PlaneGeometry(50, 50, 50, 50);
    const material = new THREE.MeshBasicMaterial({
      color: "#ffff00",
      side: THREE.DoubleSide,
    });
    const plane = new THREE.Mesh(geometry, material);
    plane.rotateX(-Math.PI / 2);
    plane.position.y = -1;
    // opts.scene.add(plane);
  }

  /**
   * 生成障碍点
   * @param num 数目
   */
  public generateBarrier(num: number): void {
    if (typeof num !== "number") {
      throw new Error("parameter can't be none-number");
    }
    for (let i = 0; i < num; i++) {
      var cubeSize = Math.ceil(Math.random() * 3);
      var cubeGeometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
      var cubeMaterial = new THREE.MeshLambertMaterial({
        color: Math.random() * 0xffffff,
      });
      var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
      cube.castShadow = true;
      cube.name = "barrier-" + i;

      cube.position.x =
        -25 + Math.round(Math.random() * 50);
      cube.position.y = cubeSize / 2 - 1;
      cube.position.z =
        -25 + Math.round(Math.random() * 50);

      // add the cube to the scene
      opts.scene.add(cube);
      this.barrierNum++;
    }
  }
}
